package com.flex.showdoc4j.generator;

import com.flex.showdoc4j.model.ReturnChildVariableModel;
import com.flex.showdoc4j.model.ReturnVariableModel;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author ganlt
 * @since 2019/10/25 12:50
 */
public abstract class AbstractGeneratorSdk implements GeneratorSdk {


    protected String getNameByUrl(String url,String suffixName) {
        int index = url.lastIndexOf("/");
        String name = url.substring(index + 1);
        int typeIndex = name.lastIndexOf(".");
        if (typeIndex != -1) {
            return firstUpperCase(name.substring(0, typeIndex + 1)+getSuffixName(suffixName));
        } else {
            return firstUpperCase(name+getSuffixName(suffixName));
        }
    }

    protected String firstUpperCase(String fileName) {
        return fileName.substring(0,1).toUpperCase()+fileName.substring(1);
    }

    private String getSuffixName(String suffixName) {
        if (StringUtils.isNotEmpty(suffixName)) {
            return suffixName;
        }
        return "";
    }

    protected void childs(List<ReturnVariableModel> returnVariableModels, List<ReturnChildVariableModel> childs) {
        for (ReturnVariableModel returnVariableModel : returnVariableModels) {
            List<ReturnVariableModel> childModel = returnVariableModel.getChildModel();
            if (childModel != null) {
                ReturnChildVariableModel returnChildVariableModel = new ReturnChildVariableModel();
                returnChildVariableModel.setName(returnVariableModel.getName());
                returnChildVariableModel.setChildModel(childModel);
                childs.add(returnChildVariableModel);
                childs(childModel, childs);
            }
        }

    }

}
